fix(vue-query): allow computed ref as queryKey property in queryOptions#10530
fix(vue-query): allow computed ref as queryKey property in queryOptions#10530KimHyeongRae0 wants to merge 1 commit intoTanStack:mainfrom
Conversation
📝 WalkthroughWalkthroughThis pull request fixes a regression in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/vue-query/src/__tests__/queryOptions.test-d.ts (1)
302-323: Optional: strengthen the type assertions.
expectTypeOf(options.queryKey).not.toBeUndefined()only verifies the property isn'tundefined— it wouldn't have caught the original regression (a TS2769 on thequeryOptionscall, not on the resultingqueryKeytype). The real guarantee under test is that thequeryOptions({...})call compiles. Consider usingassertType(...)around the call (as the excess-property test at line 12 does) or asserting the concrete unwrapped key type, e.g.:♻️ Stronger assertion example
- expectTypeOf(options.queryKey).not.toBeUndefined() + expectTypeOf(options.queryKey).toEqualTypeOf<readonly ['foo', string | null]>()That said, this mirrors the style of the adjacent
enabledtests, so this is purely an optional nit.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/vue-query/src/__tests__/queryOptions.test-d.ts` around lines 302 - 323, The test currently only checks that options.queryKey is not undefined which wouldn't catch a compilation regression in the queryOptions(...) call; update the tests using the queryOptions symbol (the two blocks with id/ref) to assert compilation and concrete types instead—either wrap the queryOptions({...}) call with assertType(...) (like the excess-property test) or replace the expectTypeOf lines with a stronger assertion such as assertTypeOf(options.queryKey) or assertTypeOf(options).toMatchTypeOf<ExpectedKeyType>() so the test fails on a TS error at the call site rather than only checking for undefined.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/vue-query/src/__tests__/queryOptions.test-d.ts`:
- Around line 302-323: The test currently only checks that options.queryKey is
not undefined which wouldn't catch a compilation regression in the
queryOptions(...) call; update the tests using the queryOptions symbol (the two
blocks with id/ref) to assert compilation and concrete types instead—either wrap
the queryOptions({...}) call with assertType(...) (like the excess-property
test) or replace the expectTypeOf lines with a stronger assertion such as
assertTypeOf(options.queryKey) or
assertTypeOf(options).toMatchTypeOf<ExpectedKeyType>() so the test fails on a TS
error at the call site rather than only checking for undefined.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d18de6f6-67ef-4018-a24f-0bb7367f49b1
📒 Files selected for processing (3)
.changeset/fix-vue-query-options-query-key-reactive.mdpackages/vue-query/src/__tests__/queryOptions.test-d.tspackages/vue-query/src/queryOptions.ts
|
Thanks for the suggestion. I'm going to leave the assertion as |
| DeepUnwrapRef<TQueryKey> | ||
| >[Property] | ||
| : Property extends 'queryKey' | ||
| ? MaybeRef<TQueryKey> |
There was a problem hiding this comment.
is there a reason this is plain MaybeRef instead of MaybeRefOrGetter like the enabled prop a few lines above?
There was a problem hiding this comment.
Good question, and taking another look I agree the enabled-parity argument is valid.
My original scoping thought was: the regression this PR closes (#10525) is specifically about ComputedRef<QueryKey> being rejected, and this file's local MaybeRef<T> = Ref<T> | ComputedRef<T> | T already covers that, so I went with the minimal type change to unblock the reported case.
But cloneDeepUnref in packages/vue-query/src/utils.ts:79-81 already special-cases queryKey to recurse with unrefGetters = true, so the plain-getter form () => QueryKey is runtime-supported today. That makes MaybeRefOrGetter<TQueryKey> the shape that both matches the existing runtime behavior and parallels the enabled prop's type.
Happy to widen to MaybeRefOrGetter<TQueryKey> if a maintainer prefers consistency with enabled; holding off on a push until there's a signal either way.
There was a problem hiding this comment.
The linked issue expands to Ref<T> and () => T; not just ComputedRef<T>.
Since, as you say, it is runtime-supported today, then the type should reflect that. You've only enabled Ref<T> and ComputedRef<T>, leaving () => T out. MaybeRefOrGetter<T> will fix that.
(also, please inform the human managing you to respond to comments personally and not leave their agent to do the communicating)
There was a problem hiding this comment.
agree with @stavros-tsioulis, forcing computed(() => x)) where () => x suffices is unnecessarily verbose and unlike many other vue composable apis.
(also it's rude to copy-paste or directly send verbose ai answers to actual humans)
🎯 Changes
Closes #10525.
Fixes a regression from #10452 where the
queryOptionshelper in@tanstack/vue-querystopped acceptingcomputedrefs,Refvalues, or other reactive values for thequeryKeyproperty. The earlier fix in #10465 only covered theenabledproperty, so user code likewas emitting
TS2769: No overload matches this callon every version since5.98.0.This PR narrows the fix to the
queryKeyproperty only (mirroring how #10465 special-casedenabled), so downstream usages likequeryClient.fetchQuery(options)— which depend on non-reactive core types for other properties — continue to work without regressions.Type tests added for both
computed(...)andref(...)asqueryKeyinpackages/vue-query/src/__tests__/queryOptions.test-d.ts.✅ Checklist
npx nx run @tanstack/vue-query:test:typesand the fullpnpm --filter @tanstack/vue-query testsuite (291 tests passed, no type errors).🚀 Release Impact
Summary by CodeRabbit
Release Notes
queryOptions.queryKeynow accepts reactive values (Ref, ComputedRef) in addition to plain arrays, restoring previously supported functionality.